/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpProvideApplication;
import com.jims.master.modules.csm.service.ExpProvideApplicationService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消耗品发放生申请Controller
 * @author zhuq
 * @version 2017-03-18
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expProvideApplication")
public class ExpProvideApplicationController extends BaseController {

	@Autowired
	private ExpProvideApplicationService expProvideApplicationService;

    /**
     * 查询申请出库数据
     * @param expProvideApplication
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String getApplicationList(ExpProvideApplication expProvideApplication, HttpServletRequest request, HttpServletResponse response, Model model) {
        expProvideApplication.setStorageOut(UserUtils.getUser().getOrgDept().getId());
        expProvideApplication.updateOrgId();
        if(null == expProvideApplication.getStorageIn() || expProvideApplication.getStorageIn().equals("")){
            model.addAttribute("list",null);
        }else{
            List<ExpProvideApplication> list = expProvideApplicationService.findApplicationExportList(expProvideApplication);
            if(null != list){
                model.addAttribute("list", list);
            }else{
                model.addAttribute("list",null);
            }
        }
        return "modules/csm/expApplicationExport";
    }

    /**
     * 申请出库数据保存
     * @param expProvideApplication
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"saveProvideApplication",""})
    @ResponseBody
    public StringData saveProvideApplication(ExpProvideApplication expProvideApplication){
        StringData data = new StringData();
        int num = expProvideApplicationService.saveProvideApplication(expProvideApplication);
        if(num > 0){
            data.setCode("success");
            data.setData("申请出库成功!");
        }else if(num == -2){
            data.setCode("error");
            data.setData("当前库存不够出库,请核实出库数量!");
        }else{
            data.setCode("error");
            data.setData("申请出库失败!");
        }
        return data;
    }

	@ModelAttribute
	public ExpProvideApplication get(@RequestParam(required=false) String id) {
		ExpProvideApplication entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expProvideApplicationService.get(id);
		}
		if (entity == null){
			entity = new ExpProvideApplication();
		}
		return entity;
	}

    /**
     * 科室申请审核页面
     * @param expProvideApplication
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     * @date 2017-06-19
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "auditProvideApplication")
    public String auditProvideApplication(ExpProvideApplication expProvideApplication,HttpServletRequest request,HttpServletResponse response,Model model){
        Page<ExpProvideApplication> page = expProvideApplicationService.searchProvideApplication(expProvideApplication,"0");
        if(null != page && null != page.getList() && page.getList().size() > 0){
            if(!page.getList().get(0).getOutFlag().equals("0")){
                page.setList(null);
            }
        }
        model.addAttribute("page",page);
        return "modules/csm/auditProvideApplication";
    }

    /**
     * 科室申请审核保存
     * @param expProvideApplication
     * @return
     * @author fengyg
     * @date 2017-06-19
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveAuditProvide")
    @ResponseBody
    public StringData saveAuditProvide(ExpProvideApplication expProvideApplication){
        StringData data = new StringData();
        int i = expProvideApplicationService.saveAuditProvide(expProvideApplication);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else if(i == -1){
            data.setCode("error");
            data.setData("审核数量不能为空!");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }

    /**
     * 查阅本库房所提申请
     * @param expProvideApplication
     * @param request
     * @param response
     * @return
     * @author fengyg
     * @date 2017-06-16
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"searchProvideApplication",""})
    public String searchProvideApplication(ExpProvideApplication expProvideApplication,HttpServletRequest request,HttpServletResponse response,Model model){
        Page<ExpProvideApplication> page = expProvideApplicationService.searchProvideApplication(expProvideApplication,"");
        model.addAttribute("page", page);
        return "modules/csm/searchProvideApplication";
    }

    /**
     * 科室申请采购首页
     * @param expProvideApplication
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     * @date 2017-06-16
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"oneIndex", ""})
    public String expProvideApplicationToOne(ExpProvideApplication expProvideApplication, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpProvideApplication> page = expProvideApplicationService.findPage(new Page<ExpProvideApplication>(request, response), expProvideApplication);
        return "modules/csm/expProvideApplicationToOneIndex";
    }

    /**
     * 科室申请采购Form页
     * @param expProvideApplication
     * @param model
     * @param request
     * @param response
     * @return
     * @author fengyg
     * @date 2017-06-16
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "oneForm")
    public String expProvideApplicationToOneForm(ExpProvideApplication expProvideApplication, Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("expProvideApplication", expProvideApplication);
        return "modules/csm/expProvideApplicationToOneForm";
    }

    /**
     * 科室申领物品首页
     * @param expProvideApplication
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     * @date 2017-06-16
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpProvideApplication expProvideApplication, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpProvideApplication> page = expProvideApplicationService.findPage(new Page<ExpProvideApplication>(request, response), expProvideApplication);
		return "modules/csm/expProvideApplicationIndex";
	}

    /**
     * 科室申领物品Form页
     * @param expProvideApplication
     * @param model
     * @param request
     * @param response
     * @return
     * @author fengyg
     * @date 2017-06-16
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpProvideApplication expProvideApplication, Model model,HttpServletRequest request,HttpServletResponse response) {
		model.addAttribute("expProvideApplication", expProvideApplication);
		return "modules/csm/expProvideApplicationForm";
	}

    /**
     * 科室申领物品(申请采购)保存
     * @param expProvideApplication
     * @param model
     * @return
     * @author fengyg
     * @date 2017-06-16
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpProvideApplication expProvideApplication, Model model) {
	    StringData data=new StringData();
		int i=expProvideApplicationService.save(expProvideApplication);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpProvideApplication expProvideApplication) {
	    StringData data=new StringData();
		int i=expProvideApplicationService.delete(expProvideApplication);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
    /**
     * 消耗品采购汇总输出pdf
     * @param expProvideApplication
     * @return com.jims.common.data.StringData
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "expProvidePrint")
    @ResponseBody
    public StringData expProvidePrint(ExpProvideApplication expProvideApplication){
        StringData stringData = new StringData();
        String printPath="";
        if("0".equals(expProvideApplication.getRemarks())){
            printPath = expProvideApplicationService.findByStorageInPrint(expProvideApplication);
        }
        else if("1".equals(expProvideApplication.getRemarks())){
            printPath = expProvideApplicationService.findByExpNamePrint(expProvideApplication);
        }
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
}